package com.bupt.cosmfl.cosmflweb.controller;

import com.bupt.cosmfl.cosmflweb.constant.Path;
import com.bupt.cosmfl.cosmflweb.constant.ResponseResult;
import com.bupt.cosmfl.cosmflweb.dto.UserLoginDTO;
import com.bupt.cosmfl.cosmflweb.dto.UserRegisterDTO;
import com.bupt.cosmfl.cosmflweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserManageController {

    @Autowired
    private UserService userService;

    @PostMapping(Path.REGISTER)
    public ResponseResult<Map<String, Integer>> register(@RequestBody UserRegisterDTO dto) {
        Map<String, Integer> res = new HashMap<>();
        Integer insertNum = userService.register(dto);
        res.put("insertNum", insertNum);
        return ResponseResult.ok(res);
    }

    @PostMapping(Path.LOGIN)
    public ResponseResult<Object> login(@RequestBody @Valid UserLoginDTO dto) {
        userService.login(dto);
        return ResponseResult.ok(null, "登录成功");
    }
}
